<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Home\Model\PaymentModel;
use Think\Controller;

class MemberController extends AdminbaseController {

    protected $memberModel;
    protected $storeModel;
    protected $orderModel;
    protected $store_relationshipModel;
    public function _initialize(){
        parent::_initialize();
        $this->memberModel = D("member");
        $this->storeModel = D("store");
        $this->orderModel = D("order");
        $this->store_relationshipModel = D("store_relationship");
    }

    //会员列表页  店铺推广展示
    public function index(){
        $post = I('');
        $start_time = $post['start_time'];
        $end_time = $post['end_time'];
        $store_parent_id = $post['store_parent_id'];
        $store_id = $post['store_id'];
        $user_name = trim($post['user_name']);
        $p = $post["p"]?$post["p"]:1;
        $pageSize = 20;

        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('store_parent_id',$store_parent_id);
        $this->assign("store_id",$store_id);

        //昨日时间
        $beginYesterday = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
        $endYesterday = mktime(0,0,0,date('m'),date('d'),date('Y'))-1;

        $where['third_class'] = array('neq',0);
        $where['fourth_class'] = 0;
        //店铺条件
        if($post['store_id']){
            //总店存在  需要把总店下属的分店所有员工遍历
            $where['first_class'] = $post['store_id'];
            //总店存在  需要把总店下属的分店遍历
            $store_parentList = $this->storeModel->where(array('parent_id'=>$post['store_id']))->select();
            $this->assign('store_parentList',$store_parentList);
            if($post['store_parent_id']){
                $where['second_class'] = $post['store_parent_id'];
            }
        }
        //员工名称搜索
        if($user_name){
            $where['user_name|real_name|phone_mob'] = array("like","%$user_name%");
            $this->assign("user_name",$user_name);
        }

        //时间条件
        if($start_time){
            $s_time = strtotime($start_time);
            $memberWhere['reg_time'] = array("egt",$s_time);
            $where_yesterday['reg_time'] = array("egt",$s_time);
            $userWhere['reg_time'] = array("egt",$s_time);
            $totalWhere['reg_time'] = array("egt",$s_time);
            $orderMember['reg_time'] = array("egt",$s_time);
            if($end_time){
                $e_time = strtotime($end_time);
                $memberWhere['reg_time'] = array("between",array($s_time,$e_time));
                $where_yesterday['reg_time'] = array("between",array($s_time,$e_time));
                $userWhere['reg_time'] = array("between",array($s_time,$e_time));
                $totalWhere['reg_time'] = array("between",array($s_time,$e_time));
                $orderMember['reg_time'] = array("between",array($s_time,$e_time));
            }
            $htime=date('H:i:s',$s_time);
            $itime=date('i',$s_time);//得到今天的分钟数
            $stime=date('s',$s_time);//得到今天的秒数
            //得到昨日的00:00:00点时间
            $beginYesterday = strtotime(date('Y-m-d H:i:s',$s_time-(24+$htime)*60*60-$itime*60-$stime));
            //得到昨天23:59:59点的时间
            $endYesterday   = strtotime(date('Y-m-d H:i:s',$s_time-($htime+1)*60*60+(59-$itime)*60+(59-$stime)));
        }else if($end_time){
            $e_time = strtotime($end_time);
            $memberWhere['reg_time'] = array("elt",$e_time);
            $where_yesterday['reg_time'] = array("elt",$e_time);
            $userWhere['reg_time'] = array("elt",$e_time);
            $totalWhere['reg_time'] = array("elt",$e_time);
            $orderMember['reg_time'] = array("elt",$e_time);
        }


        //遍历每一个员工  以及查出自己和每一个推广会员的成交金额 以及成交数
        $staffCount = $this->memberModel->where($where)->count();
//        $staffList = $this->memberModel->where($where)->page($p,$pageSize)->select();


        //查出所有的员工
        $list = $this->memberModel->where($where)->select();
        $userIds = '';          //员工总集合
        foreach($list as $k=>$v){

            if($k == 0){
                $userIds = $v['user_id'];
            }else{
                $userIds .= ','.$v['user_id'];
            }
            $list[$k]['totalTurnover'] = 0.00;
            $memberWhere['fourth_class'] = $v['user_id'];
            $orderMember['buyer_id'] = $v['user_id'];
            $orderMember['status'] = array('in',array(20,30,40));

            $list[$k]['totalNumber'] = $this->orderModel->where($orderMember)->count();
            $list[$k]['register_number'] = $this->memberModel->where($memberWhere)->count();
            $list[$k]['totalTurnover'] += sprintf("%.2f",$this->orderModel->where($orderMember)->sum('order_amount'));

        }

        $list = arraySort($list,'register_number','desc');
        if($post['register_number']){
            $list = arraySort($list,'register_number',$post['register_number']);
            $this->assign('register_number',$post['register_number']);
        }
        if($post['totalNumber']){
            $list = arraySort($list,'totalNumber',$post['totalNumber']);
            $this->assign('totalNumber',$post['totalNumber']);
        }
        if($post['totalTurnover']){
            $list = arraySort($list,'totalTurnover',$post['totalTurnover']);
            $this->assign('totalTurnover',$post['totalTurnover']);
        }
        $newData = array_values($list);
        $dataPost = array();

        for($j=($p-1)*$pageSize;$j<$p*$pageSize;$j++){
            $dataPost[$j] = $newData[$j];
            if(empty($dataPost[$j])){
                unset($dataPost[$j]);
            }
        }

        $infoUserIds = '';      //员工及员工推广的会员
        $userWhere['third_class'] = array("in",$userIds);
        $userInfo = $this->memberModel->where($userWhere)->select();
        foreach($userInfo as $j=>$i){
            if($j == 0){
                $infoUserIds = $i['user_id'];
            }else{
                $infoUserIds .= ','.$i['user_id'];
            }
        }

        //统计昨天注册会员数
        $where_yesterday['fourth_class'] = array("in",$userIds);
        $where_yesterday['reg_time'] = array("between",array($beginYesterday,$endYesterday));
        $yesterdayCount = $this->memberModel->where($where_yesterday)->count();


        $wholeMemberCount = 0;
        $totalTurnover = 0;
        //总共邀请人数
        $totalWhere['fourth_class'] = array("in",$userIds);
        $wholeMemberCount = $this->memberModel->where($totalWhere)->count();

        $orderWhere['buyer_id'] = array('in',$infoUserIds);
        //查出所有会员的合计总额   以及合计金额
        $totalSales = $this->orderModel->where($orderWhere)->sum('order_amount');
        //查出所有会员的成交总额 以及成交数量
        $orderWhere['status'] = array('in',array(20,30,40));
        $totalNumber = $this->orderModel->where($orderWhere)->count();
        $totalTurnover = $this->orderModel->where($orderWhere)->sum('order_amount');

        $page = $this->page($staffCount,$pageSize);

        //拼接返回数据
        $newArr = array(
            'list'           => $dataPost,              //会员列表数据
            'yesterdayCount' => $yesterdayCount,    //昨日注册人数
            'totalSales'     => sprintf("%.2f",$totalSales),        //合计金额
            'totalNumber'    => $totalNumber,       //已成交数量
            'totalTurnover'  => sprintf("%.2f",$totalTurnover),     //已成交金额
            'count'          => $wholeMemberCount              //共新增会员数
        );
        $storeList = $this->storeModel->where(array("parent_id"=>0))->select();
        $this->assign("newArr",$newArr);
        $this->assign("storeList",$storeList);
        $this->assign("page",$page->show('Admin'));
        $this->display();
    }

    //封装统计用户方法
    private function getMember($where){

    }
    //获取店铺
    public function getStore(){
        $store_id = I("store_id");
        $store = new StoreController();
        $list = $store->get_son_storeList($store_id);
        echo json_encode($list);
        exit();
    }


    //会员统计
    public function statement(){
        $post = I("");
        $search_arr = $this->dealwithSearchTime($post);
        //获得系统年份
        $year_arr = $this->getSystemYearArr();
        //获得系统月份
        $month_arr = $this->getSystemMonthArr();
        //获得本月的周时间段
        $week_arr = $this->getMonthWeekArr($search_arr['week']['current_year'], $search_arr['week']['current_month']);


        //店铺条件
        $storeList = $this->storeModel->where(array("parent_id"=>0))->select();
        if($post['store_id']) {
            //总店存在  需要把总店下属的分店遍历
            $store_parentList = $this->storeModel->where(array('parent_id' => $post['store_id']))->select();
            $this->assign('store_parentList',$store_parentList);
        }
        $where['fourth_class'] = array('neq',0);
        if($post['store_id']){
            $where['first_class'] = $post['store_id'];
            //搜索二级店铺
            if($post['store_parent_id']){
                $where['second_class'] = $post['store_parent_id'];
            }
        }

        //根据周时间搜索统计
        if ($search_arr['type'] == 'week') {
            $current_weekarr = explode('|', $search_arr['week']['current_week']);
            $stime = strtotime($current_weekarr[0]) - 86400 * 7;
            $etime = strtotime($current_weekarr[1]) + 86400 - 1;
            //总计的查询时间
            $count_arr['seartime'] = ($stime + 86400 * 7) . '|' . $etime;

            $up_week = @date('W', $stime); //上周
            $curr_week = @date('W', $etime); //本周
            //构造横轴数据
            for ($i = 1; $i <= 7; $i++) {
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                $tmp_weekarr = $this->getSystemWeekArr();
                //统计表数据
                $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = strtotime($current_weekarr[0]) + ($i - 1) * 86400;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 86400 - 1;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['data'][] = $tmp_weekarr[$i];
                unset($tmp_weekarr);
            }
            $where['reg_time'] = array("between",array($stime,$etime));
            $memberlist = $this->memberModel->where($where)
                ->field('count(*) as allnum ,WEEKOFYEAR(FROM_UNIXTIME(reg_time)) as weekval,WEEKDAY(FROM_UNIXTIME(reg_time))+1 as dayofweekval')->group('weekval,dayofweekval')->select();



            foreach ($memberlist as $k => $v) {
                if ($up_week == intval($v['weekval'])) {
                    $up_arr[$v['dayofweekval']] = intval($v['allnum']);
                    $uplist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
                    $count_arr['up'] += intval($v['allnum']);
                }
                if ($curr_week == $v['weekval']) {
                    $curr_arr[$v['dayofweekval']] = intval($v['allnum']);
                    $currlist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
                    $count_arr['curr'] += intval($v['allnum']);
                }
            }


            $stat_arr['series'][0]['name'] = '上周';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][0]['type'] = 'bar';
            $stat_arr['series'][0]['markPoint']['data'][0] = array('type'=>"max",'name'=>"最大值");
            $stat_arr['series'][0]['markPoint']['data'][1] = array('type'=>"min",'name'=>"最小值");
            $stat_arr['series'][0]['markLine']['data'][0] = array('type'=>"average",'name'=>"平均值");

            $stat_arr['series'][1]['name'] = '本周';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            $stat_arr['series'][1]['type'] = 'bar';
            $stat_arr['series'][1]['markPoint']['data'][0] = array('type'=>"max",'name'=>"最大值");
            $stat_arr['series'][1]['markPoint']['data'][1] = array('type'=>"min",'name'=>"最小值");
            $stat_arr['series'][1]['markLine']['data'][0]  = array('type'=>"average",'name'=>"平均值");
            $this->assign('stat_json',json_encode($stat_arr));
        };
        //根据月时间搜索统计
        if ($search_arr['type'] == 'month' || $search_arr['type'] == '') {
            $stime = strtotime($search_arr['month']['current_year'] . '-' . $search_arr['month']['current_month'] . "-01 -1 month");
            $etime = $this->getMonthLastDay($search_arr['month']['current_year'], $search_arr['month']['current_month']) + 86400 - 1;
            //总计的查询时间
            $count_arr['seartime'] = strtotime($search_arr['month']['current_year'] . '-' . $search_arr['month']['current_month'] . "-01") . '|' . $etime;

            $up_month = date('m', $stime);
            $curr_month = date('m', $etime);
            //计算横轴的最大量（由于每个月的天数不同）
            $up_dayofmonth = date('t', $stime);
            $curr_dayofmonth = date('t', $etime);
            $x_max = $up_dayofmonth < $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;
            //构造横轴数据
            for ($i = 1; $i <= $x_max; $i++) {
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                //统计表数据
                $currlist_arr[$i]['timetext'] = $i;
                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = strtotime($search_arr['month']['current_year'] . '-' . $search_arr['month']['current_month'] . "-01") + ($i - 1) * 86400;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 86400 - 1;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['data'][] = $i;
                unset($tmp_montharr);
            }

            $memberlist = $this->memberModel->where($where)
                ->field('count(*) as allnum ,MONTH(FROM_UNIXTIME(reg_time)) as monthval,day(FROM_UNIXTIME(reg_time)) as dayval ')
                ->group('monthval,dayval')
                ->select();

            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($up_month == $v['monthval']) {
                        $up_arr[$v['dayval']] = intval($v['allnum']);
                        $uplist_arr[$v['dayval']]['val'] = intval($v['allnum']);
                        $count_arr['up'] += intval($v['allnum']);
                    }
                    if ($curr_month == $v['monthval']) {
                        $curr_arr[$v['dayval']] = intval($v['allnum']);
                        $currlist_arr[$v['dayval']]['val'] = intval($v['allnum']);
                        $count_arr['curr'] += intval($v['allnum']);
                    }
                }
            }
            $stat_arr['series'][0]['name'] = '上月';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][0]['type'] = 'bar';
            $stat_arr['series'][0]['markPoint']['data'][0] = array('type'=>"max",'name'=>"最大值");
            $stat_arr['series'][0]['markPoint']['data'][1] = array('type'=>"min",'name'=>"最小值");
            $stat_arr['series'][0]['markLine']['data'][0] = array('type'=>"average",'name'=>"平均值");


            $stat_arr['series'][1]['name'] = '本月';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            $stat_arr['series'][1]['type'] = 'bar';
            $stat_arr['series'][1]['markPoint']['data'][0] = array('type'=>"max",'name'=>"最大值");
            $stat_arr['series'][1]['markPoint']['data'][1] = array('type'=>"min",'name'=>"最小值");
            $stat_arr['series'][1]['markLine']['data'][0] = array('type'=>"average",'name'=>"平均值");
            $this->assign('stat_json',json_encode($stat_arr));
        }

        $this->assign("storeList",$storeList);
        $this->assign('search_arr',$search_arr);
        $this->assign('year_arr', $year_arr);
        $this->assign('month_arr', $month_arr);
        $this->assign('week_arr', $week_arr);
        $this->display();
    }

    /**
     * 查询每月的周数组
     */
    public function getweekofmonth() {
        $year = $_POST['y'];
        $month = $_POST['m'];
        $week_arr = $this->getMonthWeekArr($year, $month);
        echo json_encode($week_arr);
        die;
    }


    /**
     * 统计个人推广人信息
    */
    public function promoters(){
        $post = I("");
        $search_arr = $this->dealwithSearchTime($post);
        //获得系统年份
        $year_arr = $this->getSystemYearArr();
        //获得系统月份
        $month_arr = $this->getSystemMonthArr();
        //获得本月的周时间段
        $week_arr = $this->getMonthWeekArr($search_arr['week']['current_year'], $search_arr['week']['current_month']);


        //店铺条件
        $storeList = $this->storeModel->where(array("parent_id"=>2,'store_type'=>"personal"))->select();

        $where['first_class'] = 2;
        $where['second_class'] = array('neq',0);
        $where['third_class'] = 0;
        $where['fourth_class'] = array('neq',0);
        //搜索二级店铺
        if($post['store_parent_id']){
            $where['second_class'] = $post['store_parent_id'];

            $memberWhere['m.user_id'] = $post['store_parent_id'];
        }
        //根据周时间搜索统计
        if ($search_arr['type'] == 'week') {
            $current_weekarr = explode('|', $search_arr['week']['current_week']);
            $stime = strtotime($current_weekarr[0]) - 86400 * 7;
            $etime = strtotime($current_weekarr[1]) + 86400 - 1;
            //总计的查询时间
            $count_arr['seartime'] = ($stime + 86400 * 7) . '|' . $etime;

            $up_week = @date('W', $stime); //上周
            $curr_week = @date('W', $etime); //本周
            //构造横轴数据
            for ($i = 1; $i <= 7; $i++) {
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                $tmp_weekarr = $this->getSystemWeekArr();
                //统计表数据
                $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = strtotime($current_weekarr[0]) + ($i - 1) * 86400;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 86400 - 1;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['data'][] = $tmp_weekarr[$i];
                unset($tmp_weekarr);
            }
            $where['reg_time'] = array("between",array($stime,$etime));
            $memberlist = $this->memberModel->where($where)
                ->field('count(*) as allnum ,WEEKOFYEAR(FROM_UNIXTIME(reg_time)) as weekval,WEEKDAY(FROM_UNIXTIME(reg_time))+1 as dayofweekval')->group('weekval,dayofweekval')->select();



            foreach ($memberlist as $k => $v) {
                if ($up_week == intval($v['weekval'])) {
                    $up_arr[$v['dayofweekval']] = intval($v['allnum']);
                    $uplist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
                    $count_arr['up'] += intval($v['allnum']);
                }
                if ($curr_week == $v['weekval']) {
                    $curr_arr[$v['dayofweekval']] = intval($v['allnum']);
                    $currlist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
                    $count_arr['curr'] += intval($v['allnum']);
                }
            }


            $stat_arr['series'][0]['name'] = '上周';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][0]['type'] = 'bar';
            $stat_arr['series'][0]['markPoint']['data'][0] = array('type'=>"max",'name'=>"最大值");
            $stat_arr['series'][0]['markPoint']['data'][1] = array('type'=>"min",'name'=>"最小值");
            $stat_arr['series'][0]['markLine']['data'][0] = array('type'=>"average",'name'=>"平均值");

            $stat_arr['series'][1]['name'] = '本周';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            $stat_arr['series'][1]['type'] = 'bar';
            $stat_arr['series'][1]['markPoint']['data'][0] = array('type'=>"max",'name'=>"最大值");
            $stat_arr['series'][1]['markPoint']['data'][1] = array('type'=>"min",'name'=>"最小值");
            $stat_arr['series'][1]['markLine']['data'][0]  = array('type'=>"average",'name'=>"平均值");
            $this->assign('stat_json',json_encode($stat_arr));
        };
        //根据月时间搜索统计
        if ($search_arr['type'] == 'month' || $search_arr['type'] == '') {
            $stime = strtotime($search_arr['month']['current_year'] . '-' . $search_arr['month']['current_month'] . "-01 -1 month");
            $etime = $this->getMonthLastDay($search_arr['month']['current_year'], $search_arr['month']['current_month']) + 86400 - 1;
            //总计的查询时间
            $count_arr['seartime'] = strtotime($search_arr['month']['current_year'] . '-' . $search_arr['month']['current_month'] . "-01") . '|' . $etime;

            $up_month = date('m', $stime);
            $curr_month = date('m', $etime);
            //计算横轴的最大量（由于每个月的天数不同）
            $up_dayofmonth = date('t', $stime);
            $curr_dayofmonth = date('t', $etime);
            $x_max = $up_dayofmonth < $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;
            //构造横轴数据
            for ($i = 1; $i <= $x_max; $i++) {
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                //统计表数据
                $currlist_arr[$i]['timetext'] = $i;
                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = strtotime($search_arr['month']['current_year'] . '-' . $search_arr['month']['current_month'] . "-01") + ($i - 1) * 86400;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 86400 - 1;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['data'][] = $i;
                unset($tmp_montharr);
            }

            $memberlist = $this->memberModel->where($where)
                ->field('count(*) as allnum ,MONTH(FROM_UNIXTIME(reg_time)) as monthval,day(FROM_UNIXTIME(reg_time)) as dayval ')
                ->group('monthval,dayval')
                ->select();
            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($up_month == $v['monthval']) {
                        $up_arr[$v['dayval']] = intval($v['allnum']);
                        $uplist_arr[$v['dayval']]['val'] = intval($v['allnum']);
                        $count_arr['up'] += intval($v['allnum']);
                    }
                    if ($curr_month == $v['monthval']) {
                        $curr_arr[$v['dayval']] = intval($v['allnum']);
                        $currlist_arr[$v['dayval']]['val'] = intval($v['allnum']);
                        $count_arr['curr'] += intval($v['allnum']);
                    }
                }
            }
            $stat_arr['series'][0]['name'] = '上月';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][0]['type'] = 'bar';
            $stat_arr['series'][0]['markPoint']['data'][0] = array('type'=>"max",'name'=>"最大值");
            $stat_arr['series'][0]['markPoint']['data'][1] = array('type'=>"min",'name'=>"最小值");
            $stat_arr['series'][0]['markLine']['data'][0] = array('type'=>"average",'name'=>"平均值");


            $stat_arr['series'][1]['name'] = '本月';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            $stat_arr['series'][1]['type'] = 'bar';
            $stat_arr['series'][1]['markPoint']['data'][0] = array('type'=>"max",'name'=>"最大值");
            $stat_arr['series'][1]['markPoint']['data'][1] = array('type'=>"min",'name'=>"最小值");
            $stat_arr['series'][1]['markLine']['data'][0] = array('type'=>"average",'name'=>"平均值");
            $this->assign('stat_json',json_encode($stat_arr));
        }
        $bankCardModel = M("bank_card");
        $memberWhere['m.is_promoters'] = 1;

        $count = $this->memberModel->alias("m")
            ->where($memberWhere)
            ->join("left join ecm_bank_card c on m.user_id = c.user_id")
            ->count();
        $page = $this->page($count, 10);
        $promotersList = $this->memberModel->alias("m")
                        ->where($memberWhere)
                        ->join("left join ecm_bank_card c on m.user_id = c.user_id")
                        ->field('c.*,m.phone_mob phone,m.user_id')
                        ->limit($page->firstRow, $page->listRows)
                        ->select();
        $this->assign('page',$page->show("Admin"));
//        $promotersList = $bankCardModel->where($memberWhere)->select();
        foreach($promotersList as $k=>&$v){
            $v['is_show'] = true;
            if(!$v['name'] || !$v['phone_mob'] || !$v['user_no']){
                $v['is_show'] = false;
            }
            if(!$v['name'])$v['name'] = '无';
            if(!$v['phone_mob'])$v['phone_mob'] = '无';
            if(!$v['bank'])$v['bank'] = '无';
            if(!$v['open_bank'])$v['open_bank'] = '无';
            if(!$v['bank_card'])$v['bank_card'] = '无';
            if(!$v['user_no'])$v['user_no'] = '无';
        }
        $this->assign('promotersList',$promotersList);


        $this->assign("storeList",$storeList);
        $this->assign('search_arr',$search_arr);
        $this->assign('year_arr', $year_arr);
        $this->assign('month_arr', $month_arr);
        $this->assign('week_arr', $week_arr);
        $this->display();
    }

    /**
     * @return bool
     */
    public function pdf(){
        $id = $_GET['id'];
        if(!$id){
            $this->error('数据传输错误！');
        }
        $bankCardModel = M("bank_card");
        $info = $bankCardModel->where(array("user_id"=>$id))->find();
        if(!$info){
            $this->error('信息有误！');
        }
        require_once '../includes/mpdf/mpdf.php';
        try{
            $mpdf=new \mPDF('zh-cn','A4', 0, '宋体', 0, 0);

            $html="<div style='padding: 40px;'><p style=\"text-align: center; width:1200px;\">
    <span style=\";font-size:15px;font-family:STSongStd-Light-Acro;color:rgb(0,0,0)\"><span style=\"color: rgb(0, 0, 0); font-size: 20px; font-family: 黑体, SimHei;\">个人网络版客户管理及咨询服务协议&nbsp;</span></span>
</p>
<p>
    <span style=\";font-size:15px;font-family:STSongStd-Light-Acro;color:rgb(0,0,0)\">保密等级：【机密】</span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">甲方： 【天津智信通电子商务有限公司】</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">地址： 【天津市河西区 </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">大沽南路与琼州道交口东南侧恒华大厦2号楼1门2109-1-5】&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">乙方： ".$info['name']." 身份证号：  ".$info['user_no']."  手机号： ".$info['phone_mob']."</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">1、 概述&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">1.1 【Cloud-Moda】APP及微信小程 </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">序：指由【天津智信通电子商务有限公司】 一家跨境电商进口零售平台公司自行开发设计的手机，电脑应用程序，甲方拥 </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">有授权范围的相关权益并享受本协议项下相关服务。&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">1.2 乙方渠道客户：凡受乙方销售渠道影响而成为APP和微信小程序用 </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">户并实现最终交易的客户均视为乙方渠道客户，包括：&nbsp; &nbsp;（1）通过乙方进行扫码、关注直接成为APP和微信小程序上消费者 </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">的客户；&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">2、 双方权利义务 甲乙双方开展合作，由乙方向甲方提供客户管理及咨询服务，甲方向乙方支付相关服务费用。 </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">甲乙双方在合作中的具体权利义务如下：</span>
</p>
<p>
    <span style=\"font-family: STSongStd-Light-Acro; font-size: 15px;\">2.1 甲方的权利义务：&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">2.1.1甲方为乙方配置APP和微信小程序平台唯一识别编码， </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">用以统计通过乙方客户管理服务所带来的销售收入，并且以乙方渠道编码在APP和微信小程序平台上的实收销售额作为乙方 </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">客户管理服务事项的结算依据，甲方的结算技术统计系统的数据采集原理等应当向乙方进行合理披露并通过该系统实现数据 </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">查询功能；&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">2.1.2甲方保证其提供的素材合法，内容不违反国家的有关政策，不侵犯第三方权利人的合法权益；&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">3、 结算及支</span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">付&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">3.1 结算依据：甲方根据乙方服务进行业绩计算，乙方服务费以APP和微信小程序实收销售额的【5】%进行结算，作为服 </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">务费向乙方进行支付。&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">3.2 结算及支付周期如下：&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">3.2.1结算及付款：双方以甲方提供的数据为结算依据。 每个账单区间为每 </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">月21日（含）到下月的20日（含）（但首个账单区间为合作开始日至第一个20日），再下个月的5号由甲方向乙方出具账单， </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">同月最后一日由甲方向乙方支付服务费。若出具账单日或者付款日为法定节假日或休息日，则顺延到下一个工作日。&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">4、 合</span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">作期限 4.1 双方首次合作期【6】个月，自双方正式开始合作日起算，到期运营正常合同可顺延。&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">5、 售后条款以及权益&nbsp;</span>
</p>
<p>
    <span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;5.1 </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">甲方对该APP和微信小程序内售卖商品质量进行管控，自行承担产品质量责任，因销售产品引起的质量问题由甲方承担相关 </span><span style=\"font-size: 15px; font-family: STSongStd-Light-Acro;\">退货、调换、赔偿等法律责任。 甲方对此协议有最终解释权。&nbsp;</span>
</p>
<p  style=\"text-align: center; width:1200px;\">
    <span>————此行以下无正文————</span>
    <img src=\"http://cloud-moda.com/ueditor/php/upload/image/20200908/1599556840390538.png\" title=\"1599556840390538.png\" alt=\"09e7b0b5f50f3a0b6ea5d63b709c40d.png\" style=\"width: 1px; height: 1px;\" width=\"1\" height=\"1\"/><img src=\"http://cloud-moda.com/ueditor/php/upload/image/20200908/1599556899136443.png\" title=\"1599556899136443.png\" alt=\"图片.png\" style=\"width: 135px; height: 130px;\" width=\"135\" height=\"130\"/>
</p></div>
";

            $html = str_replace('margin-left:100px;', '', $html);// es
            //7.0 写法
            //        $mpdf = new \mPDF(['utf-8', 'A4', 16, '', 10, 10, 15, 1]);
            $mpdf->SetDisplayMode('fullpage');
            $mpdf->autoScriptToLang = true;
            $mpdf->autoLangToFont = true;

            $mpdf->WriteHTML($html);

            $mpdf->Output(); //直接输出到页面

            $fileName='个人网络版客户管理及咨询服务协议 .pdf'; //保存到文件
            $mpdf->Output($fileName);

        }catch (Exception $e){
            return false;
        }

    }

}


